//
//  UIView+Additions.m
//  UIKit_Category
//
//  Created by Elvis on 2017/8/17.
//  Copyright © 2017年 Elvis. All rights reserved.
//

#import "UIView+Additions.h"

@implementation UIView (Additions)

#pragma mark - initilization methods

+ (instancetype)ViewWithParent:(UIView *)parent{
    return [[[self class] alloc] initWithParent:parent];
}

- (instancetype)initWithParent:(UIView *)parent{
    self = [self initWithFrame:CGRectZero];
    if (self) {
        [parent addSubview:self];
    }
    return self;
}

#pragma mark - subview
- (void)removeAllSubViews{
    for (UIView *subview in self.subviews) {
        [subview removeAllSubViews];
    }
}

#pragma mark - view operation
- (void)setBackgroundImage:(UIImage *)image{
    
    UIGraphicsBeginImageContext(self.bounds.size);
    [image drawInRect:self.bounds];
    UIImage *backgroundImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    self.backgroundColor = [UIColor colorWithPatternImage:backgroundImage];
}
- (UIImage *)covertToImage{
    
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.layer renderInContext:context];
    UIImage *toImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return toImage;
}

#pragma mark - Position of the top-left corner in superview's coordinates

- (void)setPosition:(CGPoint)position{
    CGRect frame = self.frame;
    frame.origin = position;
    [self setFrame:frame];
}

- (CGPoint)position{
    return self.frame.origin;
}

- (void)setSize:(CGSize)size{
    CGRect frame = self.frame;
    frame.size = size;
    [self setFrame:frame];
}

- (CGSize)size{
    return self.frame.size;
}

- (void)setX:(CGFloat)x{
    CGRect frame = self.frame;
    frame.origin.x = x;
    [self setFrame:frame];
}

- (CGFloat)x{
    return self.frame.origin.x;
}

- (void)setY:(CGFloat)y{
    CGRect frame = self.frame;
    frame.origin.y = y;
    [self setFrame:frame];
}

- (CGFloat)y{
    return self.frame.origin.y;
}

- (void)setTop:(CGFloat)top{
    [self setY:top];
}

- (CGFloat)top{
    return self.y;
}

- (void)setBottom:(CGFloat)bottom{
    self.y = bottom - self.height;
}

- (CGFloat)bottom{
    return self.y + self.height;
}

- (void)setLeft:(CGFloat)left{
    self.x = left;
}

- (CGFloat)left{
    return self.x;
}

- (void)setRight:(CGFloat)right{
    self.x = right - self.width;
}

- (CGFloat)right{
    return self.x + self.width;
}

- (void)setWidth:(CGFloat)width{
    CGRect frame = self.frame;
    frame.size.width = width;
    [self setFrame:frame];
}

- (CGFloat)width{
    return self.frame.size.width;
}

- (void)setHeight:(CGFloat)height{
    CGRect frame = self.frame;
    frame.size.height = height;
    [self setFrame:frame];
}

- (CGFloat)height{
    return self.frame.size.height;
}

- (void)setCenterX:(CGFloat)centerX{
    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
}

- (CGFloat)centerX{
    return self.center.x;
}

- (void)setCenterY:(CGFloat)centerY{
    CGPoint center = self.center;
    center.y = centerY;
    self.center = center;
}

- (CGFloat)centerY{
    return self.center.y;
}


#pragma mark - view layer

- (void)setBorderWidth:(CGFloat)borderWidth{
    self.layer.borderWidth = borderWidth;
}

- (CGFloat)borderWidth{
    return self.layer.borderWidth;
}

- (void)setCornerRadius:(CGFloat)cornerRadius{
    self.layer.cornerRadius = cornerRadius;
    self.layer.masksToBounds = YES;
}

- (CGFloat)cornerRadius{
    return self.layer.cornerRadius;
}

- (void)setBorderColor:(UIColor *)borderColor{
    self.layer.borderColor = borderColor.CGColor;
}

- (UIColor *)borderColor{
    return [UIColor colorWithCGColor:self.layer.borderColor];
}


#pragma mark - UIResponder

- (UIViewController *)parentViewController{
    UIResponder *responder = [self nextResponder];
    while (responder) {
        if ([responder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)responder;
        }
        responder = [responder nextResponder];
    }
    return nil;
}

@end
